package org.serverct.finalfight.internal.info;

import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.serverct.finalfight.internal.game.basic.BasicGame;
import org.serverct.finalfight.internal.game.basic.BasicGamer;
import org.serverct.finalfight.util.CustomSettings;
import org.serverct.finalfight.util.StringPair;

import java.util.UUID;

public class SimpleInfoHandler {
    public static BaseComponent[] getComponents(String text) {
        return TextComponent.fromLegacyText(text);
    }

    public static void sendActionBar(Player player, String langPath, StringPair... replacers) {
        BaseComponent[] components = getComponents(CustomSettings.getLanguageWithoutPrefix(langPath, replacers));
        player.spigot().sendMessage(ChatMessageType.ACTION_BAR, player.getUniqueId(), components);
    }

    public static void broadcastActionBar(BasicGame game, boolean ignoreDead, String langPath, StringPair... replacers) {
        BaseComponent[] components = getComponents(CustomSettings.getLanguageWithoutPrefix(langPath, replacers));
        for (UUID uuid : game.players.keySet()) {
            Player player = Bukkit.getPlayer(uuid);
            BasicGamer stat = game.players.get(uuid);
            if ((!stat.isAlive) && ignoreDead) {
                continue;
            }
            player.spigot().sendMessage(ChatMessageType.ACTION_BAR, player.getUniqueId(), components);
        }
    }

    public static void sendTitle(Player player, String titlePath, String subtitlePath, StringPair... replacers) {
        player.sendTitle(CustomSettings.getLanguageWithoutPrefix(titlePath, replacers),
                CustomSettings.getLanguageWithoutPrefix(subtitlePath, replacers),
                10, 70, 20);
    }

    public static void broadcastTitle(BasicGame game, boolean ignoreDead, String titlePath, String subTitlePath, StringPair... replacers) {
        String title = CustomSettings.getLanguageWithoutPrefix(titlePath, replacers);
        String subTitle = CustomSettings.getLanguageWithoutPrefix(subTitlePath, replacers);
        for (UUID uuid : game.players.keySet()) {
            Player player = Bukkit.getPlayer(uuid);
            BasicGamer stat = game.players.get(uuid);
            if ((!stat.isAlive) && ignoreDead) {
                continue;
            }
            player.sendTitle(title, subTitle, 10, 70, 20);
        }
    }

    public static void sendMessage(Player player, boolean hasPrefix, String langPath, StringPair... replacers) {
        String message = hasPrefix
                ? CustomSettings.getLanguageWithPrefix(langPath, replacers)
                : CustomSettings.getLanguageWithoutPrefix(langPath, replacers);
        player.sendMessage(message);
    }

    public static void broadcastMessage(BasicGame game, boolean hasPrefix, boolean ignoreDead, String langPath, StringPair... replacers) {
        String message = hasPrefix
                ? CustomSettings.getLanguageWithPrefix(langPath, replacers)
                : CustomSettings.getLanguageWithoutPrefix(langPath, replacers);
        for (UUID uuid : game.players.keySet()) {
            Player player = Bukkit.getPlayer(uuid);
            BasicGamer stat = game.players.get(uuid);
            if ((!stat.isAlive) && ignoreDead) {
                continue;
            }
            player.sendMessage(message);
        }
    }
}
